package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.*;
import com.geyue.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Chen on 2017/4/20.
 */
@RestController
@RequestMapping("/api/vedio")
public class VedioController {

    @Autowired
    private VedioService vedioService;

    @Autowired
    private FocusService focusService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private GiftService giftService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private Live_StudentService live_studentService;


    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Object all(){

        return vedioService.queryAll();
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Res list(Integer pageSize, Integer pageNum){

        return Res.success("",vedioService.getPage(pageNum,pageSize,new Vedio()));
    }

    /**
     * 点击录播，获取录播信息，以及给观看人数加1
     * @param vedioId
     * @return
     */
    @RequestMapping(value = "/getVedioById",method = RequestMethod.GET)
    @ResponseBody
    public Vedio getVedio(Integer vedioId,Integer stuId,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);
        if(student != null){
            int isMoney=vedioService.getOneByKey(vedioId).getIsMoney();

            Live_Student live_Student=live_studentService.getLive_Student(vedioId,student.getId());

            if(isMoney == 0){

                vedioService.addTotalById(vedioId);

                if(live_Student ==null){

                    Live_Student live_student=new Live_Student();
                    live_student.setLiveVedioId(vedioId);
                    live_student.setStudentId(student.getId());
                    live_student.setType(2);
                    live_student.setAddTime(new Date());
                    live_studentService.save(live_student);

                }
                return vedioService.getOneByKey(vedioId);

            }else{
                //完成付费后

                vedioService.addTotalById(vedioId);

                if(live_Student ==null){

                    Live_Student live_student=new Live_Student();
                    live_student.setLiveVedioId(vedioId);
                    live_student.setStudentId(student.getId());
                    live_student.setType(2);
                    live_student.setAddTime(new Date());
                    live_studentService.save(live_student);

                }

                return vedioService.getOneByKey(vedioId);
            }
        }else{

            return null;
        }

    }


    /**
     * 获取正在看录播的学生的头像
     */
    @RequestMapping(value = "/getStudent",method = RequestMethod.GET)
    @ResponseBody
    public Object getStudent(Integer vedioId){

        return vedioService.getStudent(vedioId,2);
    }

    /**
     * 点击关注
     */
    @RequestMapping(value = "/addFocus",method = RequestMethod.GET)
    @ResponseBody
    public String addFocus(Integer vedioId,Integer stuId,Integer type,HttpSession session){

      //  Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);

       if(student != null){
            if(type ==0){

                focusService.deleteFocus(vedioId,stuId,2);

                return "已取消关注！";

            }else{
                if(vedioId != 0){
                    Focus focus=new Focus();
                    focus.setFocusId(vedioId);
                    focus.setStudentId(stuId);
                    focus.setType(2);
                    focus.setAddTime(new Date());
                    focusService.save(focus);

                    return "关注成功！";
                }else{
                    return "关注失败！";
                }

            }


        }
        return "关注失败！";
    }

    /**
     * 点击教师头像
     */
    @RequestMapping(value = "/getTeacher",method = RequestMethod.GET)
    @ResponseBody
    public Teacher getTeacher(Integer teaId){

        return teacherService.getOneByKey(teaId);
    }

    /**
     * 添加评论
     * @param
     * @return
     */
    @RequestMapping(value = "/addComment",method = RequestMethod.GET)
    @ResponseBody
    public Res addComment(Integer vedioId,Integer stuId,HttpSession session){

      //  Student student=(Student) session.getAttribute("student");

        Student student=studentService.getOneByKey(stuId);
        if(student !=null) {
            Comment comment = new Comment();
            comment.setType(2);
            comment.setLiveVedioId(vedioId);
            comment.setStudentId(student.getId());
            comment.setAddTime(new Date());
            commentService.save(comment);
            return Res.fail("评论成功!");
        }
        return Res.fail("评论失败！");
    }

    /**
     * 根据录播ID获取评论
     * @return
     */
    @RequestMapping(value = "/getCommentById",method = RequestMethod.GET)
    @ResponseBody
    public Object getCommentById(Integer vedioId){

        return commentService.all(vedioId);

    }

    /**
     * 礼物列表
     * @return
     */
    @RequestMapping(value = "/allGift",method = RequestMethod.GET)
    @ResponseBody
    public Object allGift(){

        return giftService.queryAll();
    }

    /**
     * 礼物购买
     * @param giftId
     * @return
     */
    @RequestMapping(value = "/getGift",method = RequestMethod.GET)
    @ResponseBody
    public String getGift(Integer giftId,Integer stuId,HttpSession session){

       // Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);

        if(student != null){
            int giftMoney=giftService.getOneByKey(giftId).getMoney();
            int stuMoney=studentService.getOneByKey(student.getId()).getMoney();

            if( giftMoney >0 && stuMoney >0){

                if (giftMoney > stuMoney) {

                    return "您的学习币不足，请及时充值！";

                } else {

                    studentService.updateStudentById(student.getId(), giftService.getOneByKey(giftId).getMoney());
                    return "购买成功！";
                }
            }
            return "您的学习币不足，请及时充值！";

        }else{

            return "礼物赠送失败！";
        }


    }

    /**
     * 根据类型获取相关课程
     * @param type
     * @return
     */
    @RequestMapping(value = "/getVedioByType",method = RequestMethod.GET)
    @ResponseBody
    public Object getVedioByType (String type){

        return vedioService.getVedioByType(type);
    }

    /**
     * 相乘课程中根据教师ID获取教师信息
     * @param
     * @return
     */
    @RequestMapping(value = "/getTeacherById",method = RequestMethod.GET)
    @ResponseBody
    public Object getTeacherById(String type){


        List<Vedio> list=vedioService.getVedioByType(type);

        List<Teacher> teacherList=new ArrayList<>();

        for (Vedio item: list ) {

            int id=item.getTeacherId();

            teacherList.add(teacherService.getOneByKey(id));
        }
        return teacherList;
    }


    /**
     * 录播搜索
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    @ResponseBody
    public Object search(String keyword){

            return vedioService.searchVedioByKeyword(keyword);


    }

    /**
     * 退出录播时，观看人数减少1
     * @return
     */
    @RequestMapping(value = "/deleteVedioTotal",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteVedioTotal(HttpSession session,Integer vedioId,Integer stuId){

        //Student student=(Student) session.getAttribute("student");
        Student student=studentService.getOneByKey(stuId);

        live_studentService.deleteLiveStudent(vedioId,student.getId(),2);

         return vedioService.decreaseTotalById(vedioId);
    }




}
